package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.xml.soap.Detail;
import java.util.List;

@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    /**
     * spring容器启动，并且初始化之后，马上执行的操作
     * @param contextRefreshedEvent
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        System.out.println("---------------------------------------- vo数据初始化 start ------------------------------------------------");


        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail detail : list) {
            //如果vo已经存在了，跳过，防止再次初始化，覆盖数据
            if(strategyStatisVOService.isStrategyVOExist(detail.getId())){
                continue; //跳过就好
            }

            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtils.copyProperties(detail,vo);
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setStrategyId(detail.getId());

            strategyStatisVOService.setStrategyStatisVO(vo);
        }

        System.out.println("---------------------------------------- vo数据初始化 end --------------------------------------------------");

        System.out.println("---------------------------------------- zset数据初始化 end --------------------------------------------------");

        //热门初始化
        for (StrategyDetail detail : list) {

            String voId = RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString());
            String zsetKey = RedisKeys.STRATEGY_HOT_ZSET.getPrefix();
            //判断voId是否zai在指定的zset中
            if(strategyStatisVOService.isVoidExsitZset(voId,zsetKey)){
                continue;
            }

            int score = detail.getReplynum()+detail.getViewnum();
            strategyStatisVOService.addScoreHot(detail.getId(),score);
        }
        //国内外操作
        for (StrategyDetail detail : list) {

            String voId = RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString());
            String zsetKey = null;
            if(detail.isIsabroad()){
                //国外
                zsetKey = RedisKeys.STRATEGY_ABROAD_ZSET.getPrefix();
            }else{
                //国内
                zsetKey = RedisKeys.STRATEGY_CHINA_ZSET.getPrefix();
            }



            //判断voId是否zai在指定的zset中
            if(strategyStatisVOService.isVoidExsitZset(voId,zsetKey)){
                continue;
            }
            //点赞和收藏
            int score = detail.getThumbsupnum()+detail.getFavornum();
            strategyStatisVOService.addScoreAbroadAndChina(detail.getId(),score);
        }











        System.out.println("---------------------------------------- zset数据初始化 end --------------------------------------------------");


    }
}
